

//
//  BackgroundRequestManager.m
//  MXApp
//
//  Created by CC on 2017/9/7.
//  Copyright © 2017年 Shanghai YingXiang Network Technology Co., Ltd. All rights reserved.
//

#import "BackgroundRequestManager.h"
#import "NetWorkEngine.h"
#import "LcNotificationCenter.h"
#import "NotificationHint.h"

@interface BackgroundRequestManager ()
@property(nonatomic, strong) NSThread* thread;
@property(nonatomic, strong) dispatch_semaphore_t semaphore;
@property(nonatomic, strong) LcNotificationCenter* notificationCenter;
@property(nonatomic, strong) NetWorkEngine* requestGroup;
@end

@implementation BackgroundRequestManager

- (void)internalInit {
  self.semaphore = dispatch_semaphore_create(0);
  self.notificationCenter = [LcNotificationCenter sharedInstance];
  self.requestGroup = [[NetWorkEngine alloc] init];
}

- (void)startBackgroundThread {
  self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
  [self.thread start];
}

- (void)run {
  while (true) {
    @autoreleasepool {
      // LcLog(@"BackgroundRequestManager");
      [NSThread sleepForTimeInterval:10];
//      if (!hasLogin()) {
//        continue;
//      }
//      @synchronized(self) {
//        [self.requestGroup POST:MXURL_USER_MSGUNREADCOUNT
//            parameters:@{
//              @"guideId" : strOrEmpty(getLoginInfo().userId)
//            }
//            success:^(NSURLSessionDataTask* task, id responseObject) {
//              dispatch_semaphore_signal(self.semaphore);
//              [responseObject get_CompleteWithResult:^(id completeDataDict, BOOL success) {
//                if (success) {
//                  NSInteger unReadCount = [completeDataDict[@"totalCount"] integerValue];
//                  [self.notificationCenter sendNotification:NEW_SYSTEMMESSAGE_NOTIFICATION auxiliary:@(unReadCount)];
//                }
//              }];
//            }
//            failure:^(NSURLSessionDataTask* task, NSError* error){
//              dispatch_semaphore_signal(self.semaphore);
//            }];
//        dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
//      }
    }
  }
}

@end
